import {request} from '../../request/index';
import regeneratorRuntime from '../../lib/runtime/runtime.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: [
      {id:0, title:'综合',isActive:true},
      {id:0, title:'销量',isActive:false},
      {id:0, title:'价格',isActive:false},
    ],
    // 要渲染的商品数组
    goodsList: []
    
  },
  QueryParams: {
    query: "",
    cid: '',
    pagenum: 1,
    pagesize: 10
  },
  // 总页数
  totalPage: 1,

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    // console.log(options)
    this.QueryParams.cid=options.cid;
   this.getGoodsList();

  },
  // tab栏
  itemChange(e){
    const {index} = e.detail;
    let {tabs} = this.data;
    tabs.forEach((v,i) => i===index?v.isActive=true:v.isActive=false);
     this.setData({
       tabs
     });
  },
// 获取商品数据
  async getGoodsList(){
      const result = await  request({url:'/goods/search',data:this.QueryParams});
        console.log(result);
              //  计算总页数
      this.totalPage=Math.ceil(result.total/this.QueryParams.pagesize);
       console.log(this.totalPage);
    this.setData({
     goodsList:[...this.data.goodsList,...result.goods]
    });
    wx.stopPullDownRefresh();
   },
  //  页面上拉触底
  onReachBottom(){
    if(this.QueryParams.pagenum>=this.totalPage){
     wx.showToast({
       title: '没有下一页了',
       icon: 'none'
     });
    }else{
      // console.log('有下一页')
      this.QueryParams.pagenum++;
      this.getGoodsList();
    }
  },
  // 上滑刷新
  onPullDownRefresh(){
    this.QueryParams.pagenum=1;
    this.setData({
      goodsList: [],
    });
    // 从新获取数据
    this.getGoodsList();
  }

})